package com.time.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.time.domain.Order;
import com.time.domain.OrderDetail;
import com.time.domain.Product;
import com.time.domain.User;
import com.time.service.OrderService;
import com.time.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 周杰
 * @Description 订单信息 controller
 * @since 2020-10-21
 */

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * @param model 封装订单高级查询回显数据和分页订单数据
     * @param order 订单高级查询对象
     * @param page  当前页和每页显示数量
     * @return 请求转发至后台订单列表页面
     * @Description 订单号或用户名高级+分页查询所有订单数据给后台
     */
    @RequestMapping("/list")
    public String order(Model model, Order order, Page<Order> page) {
        // 由于页面无法选择每页显示数据，所以默认每页显示4条留言数据
        page.setSize(4);
        Page<Order> orderPage =
                orderService.orderByIdOrUserNamePage(order.getId(),
                        order.getUserName(), page);

        model.addAttribute("order", order);
        model.addAttribute("orderPage", orderPage);
        model.addAttribute("totalPage", orderPage.getPages());
        return "manage/order";
    }

    /**
     * @param model 封装回显的订单数据
     * @param id    订单ID
     * @return 请求转发至订单编辑页面
     * @Description 请求转发至订单更新页面
     */
    @RequestMapping("/update_input")
    public String input(Model model, Integer id) {
        Order order = orderService.getById(id);

        model.addAttribute("order", order);
        return "manage/order-modify";
    }

    /**
     * @param order 订单对象
     * @return 根据更新结果, 请求转发至相应页面
     * @Description 更新订单状态数据
     */
    @PostMapping("/update")
    public String update(Order order) {
        boolean updateOrder = orderService.updateById(order);

        // 根据更新结果,请求转发至相应页面
        return JumpPage.backJumpPage(updateOrder);
    }

    /**
     * @return 请求转发至购物页面
     */
    @RequestMapping("/shopping")
    public String shopping() {
        return "/shopping";
    }

    /**
     * @param session 获取用户session和购物车session数据
     * @return 请求转发至成功购买页面
     * @Description 将购物车商品生产订单
     */
    @RequestMapping("/shopping_result")
    public String shoppingResult(HttpSession session) {

        // 获取用户和购物车信息
        List<Product> products = SessionUtils.getShoppingCartSession(session);
        User user = SessionUtils.getUserSession(session);

        // 初始化订单
        Order order = new Order();
        order.setUserId(user.getUserId());
        order.setUserName(user.getUserName());
        order.setUserAddress(user.getAddress());
        order.setStatus(1);
        order.setType(1);
        BigDecimal cost = new BigDecimal("0");

        // 初始化订单详细数据
        for (Product product : products) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setPId(product.getId());
            orderDetail.setQuantity(product.getCount());
            orderDetail.setCost(product.getSubtotal());
            order.getOrderDetails().add(orderDetail);
            cost = cost.add(new BigDecimal(product.getPrice().toString())
                    .multiply(new BigDecimal(product.getCount().toString())));
        }
        order.setCost(cost);
        // 添加订单
        orderService.addOrder(order);

        SessionUtils.removeShoppingCartSession(session);
        return "/shopping-result";
    }

    /**
     * @param id 订单ID
     * @return 根据删除结果请求转发至相应页面
     * @Description 根据订单ID删除订单数据
     */
    @RequestMapping("/delete")
    public String delete(Integer id) {
        boolean deleteOrder = orderService.removeById(id);

        // 根据删除结果请求转发至相应页面
        return JumpPage.backJumpPage(deleteOrder);
    }
}
